import axios from "axios";

const login_url = "/api_2/api/users/login";
const register_url = "/api_2/api/users/register";
const editUser_url = "/api_2/api/users/info/edit/";
const getProfile_url = "/api_2/api/profiles";
const add_url = "/api_2/api/profiles/";
const delete_url = "/api_2/api/profiles/delete/";

//登录请求
function login(data) {
  return new Promise((resolve, reject) => {
    axios.post(login_url, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}

//注册请求
function register(data) {
  return new Promise((resolve, reject) => {
    axios.post(register_url, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      })
  });
}

//修改用户信息
function editUser(option, newData) {
  return new Promise((resolve, reject) => {
    axios.post(editUser_url + option, newData)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      })
  });
}

// 查看 数据请求
function getProfile() {
  return new Promise((resolve, reject) => {
    axios.get(getProfile_url)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      })
  });
}

// 添加/编辑保存 数据请求
function add(option, data) {
  return new Promise((resolve, reject) => {
    axios.post(`${add_url}${option}`, data)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      })
  });
}

// 删除数据请求
function deleteData(option) {
  return new Promise((resolve, reject) => {
    axios.delete(`${delete_url}${option}`)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}

export {login, register, editUser, getProfile, add, deleteData};